/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package it.tukano.swingdeco.table;

import it.tukano.check.Todo;
import it.tukano.fun.Function0;
import it.tukano.swingdeco.containers.JFrameExt;
import it.tukano.swingdeco.containers.JPanelExt;
import it.tukano.swingdeco.controls.JCheckBoxExt;
import it.tukano.swingdeco.wizard.WizardDialog;
import java.awt.BorderLayout;
import java.awt.Component;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import javax.swing.JComboBox;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author utente4
 */
public class JTableFilterDialog {
    
    public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            
            public void run() {
                JTableExt table = new JTableExt(5,5);
                JFrameExt frame = new JFrameExt();
                frame.add(table.getScroller());
                frame.defaultDispose();
                frame.packAndShow();
                new JTableFilterDialog(table).show(frame);
            }
        });
    }
    
    private final List<String> filteredColumns = new ArrayList<String>();
    private final JTable table;
    private final WizardDialog dialog = new WizardDialog();
    private final JPanelExt columns;
    private final JTableExt filterExpressionTable = new JTableExt();

    public JTableFilterDialog(JTable table) {
        this.table = table;
        
        columns = new JPanelExt(table.getColumnCount(), 1).withName("buttons");
        final JPanelExt card001 = new JPanelExt(new BorderLayout())
                .setTitledBorder("Campi da esaminare")
                .addComponent(BorderLayout.CENTER, columns);
        int columnCount = table.getColumnCount();
        for (int i = 0; i < columnCount; i++) {
            String columnName = table.getColumnName(i);
            columns.add(new JCheckBoxExt(columnName));
        }
        final JPanelExt card002 = new JPanelExt(new BorderLayout())
                .setTitledBorder("Impostazione criteri")
                .addComponent(BorderLayout.CENTER, filterExpressionTable.getScroller());
        
        dialog.addCard(card001, BorderLayout.CENTER, new Function0<Boolean>() {

            public Boolean apply() {
                return !getChosenColumns().isEmpty();
            }
        });
        dialog.addCard(card002, BorderLayout.CENTER, new Function0<Boolean>() {

            public Boolean apply() {
                return true;
            }
        });
        dialog.executeOnCardWillShow(card001, new Runnable() {

            public void run() {
                filteredColumns.clear();
                filteredColumns.addAll(getChosenColumns());
            }
        });
        dialog.executeOnCardWillShow(card002, new Runnable() {

            public void run() {
                DefaultTableModel model = new DefaultTableModel(filteredColumns.size(), 2);
                model.setColumnIdentifiers(filteredColumns.toArray());
                filterExpressionTable.setModel(model);
            }
        });
    }
    
    private List<String> getChosenColumns() {
        LinkedList<String> list = new LinkedList<String>();
        for (final Component component : columns.getComponents()) {
            JCheckBoxExt box = (JCheckBoxExt) component;
            if(box.isSelected()) list.add(box.getName());
        }
        return list;
    }
    
    public JTableFilter show() {
        return show(table);
    }
    
    public JTableFilter show(Component parent) {
        dialog.showDialog(parent, "Crea filtro", null, new Runnable() {

            public void run() {
                Todo.implementThis();
            }
        });
        return null;
    }
    
    private final String[] TEXT_FILTER_LABELS = {"Contiene", "Uguale a", "Non contiene", "Diverso da"};
    private final String[] DATE_FILTER_LABELS = {"Uguale a", "Precede il", "Segue il"};
    private final String[] NUMERIC_FILTER_LABELS = {"Uguale a", "Maggiore di", "Minore di"};
    private final String[] TYPE_FILTER_LABELS = {"Numero", "Data", "Testo"};
    
    class TypeFilterBox extends JComboBox {
        //Default serial version code
        private static final long serialVersionUID = 0;
        
        public TypeFilterBox() {
            super(TYPE_FILTER_LABELS);
        }
    }
    
    class TextFilterBox extends JComboBox {
        //Default serial version code
        private static final long serialVersionUID = 0;
        
        public TextFilterBox() {
            super(TEXT_FILTER_LABELS);
        }
    }
    
    class DateFilterBox extends JComboBox {
        //Default serial version code
        private static final long serialVersionUID = 0;
        
        public DateFilterBox() {
            super(DATE_FILTER_LABELS);
        }
    }
    
    class NumericFilterBox extends JComboBox {
        //Default serial version code
        private static final long serialVersionUID = 0;
        
        public NumericFilterBox() {
            super(NUMERIC_FILTER_LABELS);
        }
    }
}
